package com.hs.carrental.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hs.carrental.common.Result;
import com.hs.carrental.dto.LoginDto;
import com.hs.carrental.dto.admin.AdminDeleteDto;
import com.hs.carrental.dto.admin.AdminPageDto;
import com.hs.carrental.dto.admin.AdminSaveDto;
import com.hs.carrental.service.IAdminService;
import com.hs.carrental.vo.AdminLoginVo;
import com.hs.carrental.vo.admin.AdminPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyy
 * @since 2021-03-17
 */
@RestController
@RequestMapping("/carrental/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    /**
     * 编辑或新增管理员
     * @return
     */
    @PostMapping("/save")
    public Result<Object> save(@RequestBody AdminSaveDto adminSaveDto) throws UnsupportedEncodingException {
        return adminService.save(adminSaveDto)?Result.ok("操作成功"):Result.failed("操作失败");
    }

    /**
     * 删除
     * @param adminDeleteDto
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody AdminDeleteDto adminDeleteDto){
        if (adminService.removeById(adminDeleteDto.getAdminId())){
            return Result.ok("删除成功");
        }
        return Result.failed("删除失败");
    }

    /**
     * 分页查询管理员
     * @param adminPageDto
     * @return
     */
    @PostMapping("/getPage")
    public Result getPage(@RequestBody AdminPageDto adminPageDto){
        IPage<AdminPageVo> page = adminService.getPage(adminPageDto);
        return Result.ok(page);
    }
}
